var util = require('../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    actId: '',
    openId: '',
    shareId: '',
    act: '',
    helps:[],
    phoneNumber:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this = this;
    if (options.id) {
      _this.setData({
        actId: options.id,
      })
    }
    if (options.openId) {
      _this.setData({
        openId: options.openId,
      })
    }
    _this.findInfo()
  },
  /**
     * 查询活动详情
     */
  findInfo: function () {
    var _this = this;
    util.request('/wxapi/helpActivity/queryShare/' + _this.data.actId + '/' + _this.data.openId, {},        function (data) {
      //如果已助力完成未领取奖品 
      var share = data.rows[0]
        if (share.shaStatus == '2') {
          _this.setData({ help: true, disModel: 3, });
          var helps = data.rows[0].helps
          _this.setData({
            act: data.rows[0].activity,
            helps: helps,
            shareId: share.shaId,
          })
        }
    }, true);
  },


  /*获取手机号码**/
  getPhoneNumber: function (e) {
    if (e.detail.errMsg == "getPhoneNumber:fail user deny") {
      wx.showToast({
        title: '取消授权',
        duration: 2000,
        image: "/image/fail.png"
      })
      return;
    }
    //请求解密数据
    var _this = this;
    util.request('/wxapi/helpActivity/deciphering', e.detail, function (data) {
      console.log(data);
      var result = JSON.parse(data.mapInfo.decipher);
      if (result == null || result.phoneNumber == null) {
        wx.showToast({
          title: '获取手机号码失败',
          duration: 2000,
          image: "/image/fail.png"
        })
      }
      _this.setData({
        phoneNumber: result.phoneNumber
      })

    }, true);
  },

  /**
 * 表单提交
 */
  formSubmit: function (e) {
    console.log('领取礼品：', e)
    var subData = e.detail.value;
   
    subData.formId = e.detail.formId
    var _this = this;
    util.request('/wxapi/helpActivity/receiveSharePrize',
      subData,
      function (data) {
        console.log('订单信息：', data.rows[0])
        wx.redirectTo({
          url: '../customerBenefit/benefit?id=' + data.rows[0].orderId,
          success: function () { },        //成功后的回调；
        })
      }, true);

  },
})